<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="initial-scale=1, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0" />
  <title>JRoll下拉刷新 for Vue2</title>
  <style>
    html,
    body {
      margin: 0;
      padding: 0;
      height: 100%;
      background: #dadada;
    }

    header {
      height: 44px;
      line-height: 44px;
      text-align: center;
      background: #da020c;
      color: #fff;
    }

    header>a {
      height: 34px;
      line-height: 34px;
      position: absolute;
      top: 5px;
      left: 10px;
    }

    h1 {
      margin: 0;
      padding: 0;
      font-size: 18px;
      font-weight: normal;
    }

    #app {
      position: absolute;
      top: 44px;
      bottom: 0;
      width: 100%;
    }

    #wrapper {
      position: absolute;
      top: 44px;
      bottom: 0;
      width: 100%;
    }

    #scroller {
      margin: 0;
      padding: 0;
    }

    .item {
      height: 60px;
      line-height: 60px;
      border-bottom: 1px solid #ddd;
      text-indent: 1em;
      background: #fff;
    }

    .jroll-infinite-tip {
      height: 44px;
      line-height: 44px;
      text-align: center;
    }

    .jroll-vue-infinite {
      height: 100%;
    }
  </style>
  <script src="../../vendor/vue.js"></script>
  <script src="../../src/jroll.js"></script>
  <script src="jroll-vue-infinite.js"></script>
  <script src="../jroll-pulldown/jroll-pulldown.js"></script>
</head>

<body>
  <header>
    <h1>JRoll下拉刷新 for Vue2</h1>
    <a onclick="javascript:history.back()">&lt;返回</a>
  </header>
  <div id="app">
      <jroll-infinite class="jroll-vue-infinite">
          <div class='item' v-for="i in items">{{i.index}}、{{i.text}}</div>
      </jroll-infinite>
  </div>

  <script>
    //模拟ajax
    function ajax(parms) {
      var a = [],
        i;
      var page = parms.url.replace("getData.do?page=", "");
      for (i = 1; i <= 20; i++) {
        a.push({
          "index": i + (page - 1) * 20,
          "text": "普通分页加载模式"
        });
      }
      setTimeout(function() {
        parms.success(a);
      }, 800);
    }

    // vue实例
    var vm
    var infOptions = {
      pulldown: {},
      bottomed: function (complete) {
        var me = this
        ajax({
          url: 'getData.do?page=' + (me.page + 1),
          success: function (data) {
            // 加入下拉刷新功能后需要判断complete是否为function类型，是，表示刷新，复位第1页；否，表示上拉加载下一页，拼接下一页数据
            if (typeof complete === 'function') {
              vm.$data.items = data
              complete()
            } else {
              vm.$data.items = vm.$data.items.concat(data)
            }
            me.success()
          }
        })
      },
      updated: function () {
        console.log('current page is ' + this.page)
      }
    }

    vm = new Vue({
      el: '#app',
      components: {
        'jroll-infinite': JRoll.VueInfinite(infOptions, { scrollBarY: true })
      },
      data: {
        items: []
      }
    })
  </script>
</body>

</html>